home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 2.iso
/
STUTTGART
/
TEMP
/
GNU
/
bison
/
Contextual
< prev
next >
Wrap
Text File
|
1995-06-28
|
2KB
|
60 lines
Contextual Precedence
Previous: <Precedence=>Precedencf> * Next: <Parser States=>ParserStat> * Up: <Algorithm=>Algorithm>
#Wrap on
{fH3}Context-Dependent Precedence{f}
Often the precedence of an operator depends on the context. This sounds
outlandish at first, but it is really very common. For example, a minus
sign typically has a very high precedence as a unary operator, and a
somewhat lower precedence (lower than multiplication) as a binary operator.
The Bison precedence declarations, {fCode}%left{f}, {fCode}%right{f} and
{fCode}%nonassoc{f}, can only be used once for a given token; so a token has
only one precedence declared in this way. For context-dependent
precedence, you need to use an additional mechanism: the {fCode}%prec{f}
modifier for rules.
The {fCode}%prec{f} modifier declares the precedence of a particular rule by
specifying a terminal symbol whose precedence should be used for that rule.
It's not necessary for that symbol to appear otherwise in the rule. The
modifier's syntax is:
#Wrap off
#fCode
%prec {fStrong}terminal-symbol{f}
#f
#Wrap on
and it is written after the components of the rule. Its effect is to
assign the rule the precedence of {fStrong}terminal-symbol{f}, overriding
the precedence that would be deduced for it in the ordinary way. The
altered rule precedence then affects how conflicts involving that rule
are resolved (\*Note <Precedence=>Precedencf>: Operator Precedence).
Here is how {fCode}%prec{f} solves the problem of unary minus. First, declare
a precedence for a fictitious terminal symbol named {fCode}UMINUS{f}. There
are no tokens of this type, but the symbol serves to stand for its
precedence:
#Wrap off
#fCode
…
%left '+' '-'
%left '\*'
%left UMINUS
#f
#Wrap on
Now the precedence of {fCode}UMINUS{f} can be used in specific rules:
#Wrap off
#fCode
exp: …
| exp '-' exp
…
| '-' exp %prec UMINUS
#f
#Wrap on